// 广告排序分析图
<template>
    <div class="gradient-content full-width full-height">
        <div :id="idName"></div>
    </div>
</template>

<script>
import { Chart } from '@antv/g2'
export default {
    props: {
        idName: {
            type: String,
            default: 'SendStock',
        },
        chartData: {
            type: Object,
            default: () => {},
        },
    },
    watch: {
        chartData: {
            handler(val) {
                if (val.have_send_stock_rate) {
                    this.$nextTick(() => {
                        if (this.height == 0) {
                            const boxContElement = document.querySelector(`.gradient-content`)
                            this.height = boxContElement.clientHeight
                        }

                        this.render()
                    })
                }
            },
            immediate: true,
            deep: true,
        },
    },
    data() {
        return {
            chart: null,
            height: 0,
        }
    },
    mounted() {
        this.$nextTick(() => {
            const boxContElement = document.querySelector(`.gradient-content`)
            this.height = boxContElement.clientHeight

            // this.render()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
            const data = [
                { item: '缺货未发', percent: parseFloat(this.chartData.out_send_stock_rate), count:parseFloat(this.chartData.out_send_stock) },
                { item: '有货待发', percent: parseFloat(this.chartData.have_send_stock_rate), count:parseFloat(this.chartData.have_send_stock) },
            ]
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                autoFit: true,
                padding: [10, 10, 30, 10],
            })
            // Step 2: 载入数据源
            this.chart.data(data)
            this.chart.scale('percent', {
                formatter: val => {
                    return val + '%'
                },
                style: {
                    fill: '#fff',
                },
            })
            this.chart.coordinate('theta', {
                radius: 0.85,
                innerRadius: 0.75,
            })
            this.chart.tooltip({
                showTitle: false,
                showMarkers: false,
                itemTpl: '<li class="g2-tooltip-list-item"><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>',
            })
            this.chart
                .annotation()
                .text({
                    position: ['50%', '50%'],
                    content: '当前未发订单数',
                    style: {
                        fontSize: 12,
                        fill: '#fff',
                        textAlign: 'center',
                    },
                    offsetY: -10,
                })
                .text({
                    position: ['50%', '50%'],
                    content: this.chartData.not_send_order,
                    style: {
                        fontSize: 20,
                        fill: '#fff',
                        textAlign: 'center',
                    },
                    offsetY: 10,
                })
            this.chart.legend({
                // position: 'right',
                itemName: {
                    style: {
                        fill: '#fff',
                    },
                },
                pageNavigator: {
                    marker: {
                        style: {
                            fill: '#fff',
                            inactiveFill: '#fff',
                        },
                    },
                },
            })
            this.chart
                .interval()
                .adjust('stack')
                .position('percent')
                .color('item')
                .label('percent', percent => {
                    return {
                        content: data => {
                            return `${data.item}: ${data.count}  ${percent}%`
                        },
                        style: {
                            fill: '#fff',
                        },
                        position: (data, mappingData, index) => {
                            console.log('position', data,mappingData, index);
                            // if (data.x > 0) {
                            return 'top';
                            // }
                            // return 'bottom';
                        }
                    }
                })
                .tooltip('item*percent', (item, percent) => {
                    return {
                        name: item,
                        value: percent + '%',
                    }
                })
            this.chart.interaction('element-active')
           
            // Step 4: 渲染图表
            this.chart.render()
        },
    },
}
</script>
